package javao.cn.work.orc.service.impl;

import javao.cn.work.orc.common.constants.rect.RectAF150Constants;
import javao.cn.work.orc.pojo.survey.OrcSurvey;
import javao.cn.work.orc.service.AF150SurveyService;
import javao.cn.work.orc.utils.orc.OcrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 
 * @Author Admin小闫
 * @Date 2024/12/6
 */
@Service
public class AF150SurveyServiceImpl implements AF150SurveyService {

    @Autowired
    private OcrUtils ocrUtils;

    /**
     * 传入图片流识别 选择测量值
     * @param images 图片流
     * @return 识别结果
     */
    @Override
    public OrcSurvey orcSurvey(Map<Integer, BufferedImage> images){
        OrcSurvey orcSurvey=new OrcSurvey();
        List<Map<String, Map<String, Map<String,String>>>> list=new ArrayList<>();
        for (Map.Entry<Integer, BufferedImage> entry : images.entrySet()) {
            Integer key = entry.getKey();
            BufferedImage Image = entry.getValue();
            //首页的图片格式
            if (key==0){
                //取出首页的6个值
                orcSurvey.setPartName(ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_PART_NAME));
                orcSurvey.setPartNo(ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_PART_NO_KW));
                orcSurvey.setPartCounter(ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_PART_COUNTER));
                orcSurvey.setOperator(ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_OPERATOR));
                orcSurvey.setOperator_AF(ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_OPERATOR_AF));
                orcSurvey.setDate(ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_DATE));
                orcSurvey.setTime(ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_TIME));
                //第一页的第一组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap = orcReadValue(Image, 0);
                if (stringOrcMap.size()<1){
                    break;
                }
                list.add(stringOrcMap);
            }
            if (key==1){
                //第二页的第一组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap1 = orcReadValue(Image, 1);
                if (stringOrcMap1.size()<1){
                    break;
                }
                list.add(stringOrcMap1);
                //第二页的第二组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap2 = orcReadValue(Image, 2);
                if (stringOrcMap2.size()<1){
                    break;
                }
                list.add(stringOrcMap2);
            }
            if (key==2){
                //第三页的第一组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap3 = orcReadValue(Image, 3);
                if (stringOrcMap3.size()<1){
                    break;
                }
                list.add(stringOrcMap3);
            }
            if (key==3){
                //第四页的第一组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap4 = orcReadValue(Image, 4);
                if (stringOrcMap4.size()<1){
                    break;
                }
                list.add(stringOrcMap4);
                //第四页的第二组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap5 = orcReadValue(Image, 5);
                if (stringOrcMap5.size()<1){
                    break;
                }
                list.add(stringOrcMap5);
            }
            if (key==4){
                //第五页的第一组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap6 = orcReadValue(Image, 6);
                if (stringOrcMap6.size()<1){
                    break;
                }
                list.add(stringOrcMap6);
                //第五页的第二组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap7 = orcReadValue(Image, 7);
                if (stringOrcMap7.size()<1){
                    break;
                }
                list.add(stringOrcMap7);
            }
            if (key==5){
                //第六页的第一组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap8 = orcReadValue(Image, 8);
                if (stringOrcMap8.size()<1){
                    break;
                }
                list.add(stringOrcMap8);
                //第六页的第二组数据
                Map<String, Map<String, Map<String,String>>> stringOrcMap9 = orcReadValue(Image, 9);
                if (stringOrcMap9.size()<1){
                    break;
                }
                list.add(stringOrcMap9);
            }
        }
        orcSurvey.setSurveyValue(list);
        return orcSurvey;
    }

    /**
     * 读取每组值
     * @param Image
     * @param i
     * @return
     */
    private Map<String, Map<String, Map<String,String>>> orcReadValue(BufferedImage Image, int i){
        Map<String, Map<String, Map<String,String>>> map=new HashMap<>();
        Map<String, Map<String,String>> orcAF90RzMap=new HashMap<>();
        String s = ocrUtils.performOCRByRegion(Image, RectAF150Constants.AF150_SURVEY_VALUE[i][0]);

        if (s.equals("MP_HLS5")){
            s=s.replace("MP_HLS5","MP_HL5");
        }

        if (s==null||s.equals("")){//如果没识别到组号，则代表本页结束了，不需要往下识别返回空集合
            return map;
        }

        if (i==9){//AF150中 9代表最后一页的最后三个值跟其他不同
            //识别ORC_RZ
            Map<String,String> orcRZ=new HashMap<>();
            orcRZ.put("act",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][1]));
            orcRZ.put("lt",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][2]));
            orcRZ.put("ut",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][3]));
            orcRZ.put("<>",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][4]));
            orcAF90RzMap.put("Rz",orcRZ);

            //识别ORC_Rt
            Map<String,String> orcRt=new HashMap<>();
            orcRt.put("act",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][4]));
            orcRt.put("lt",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][5]));
            orcRt.put("ut",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][6]));
            orcRt.put("<>",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][7]));
            orcAF90RzMap.put("Rt",orcRt);

            //识别ORC_Rmax
            Map<String,String> orcRmax=new HashMap<>();
            orcRmax.put("act",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][4]));
            orcRmax.put("lt",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][5]));
            orcRmax.put("ut",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][6]));
            orcRmax.put("<>",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][7]));
            orcAF90RzMap.put("Rmax",orcRmax);

        }else {
            //识别ORC_RZ
            Map<String,String> orcRZ=new HashMap<>();
            orcRZ.put("act",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][1]));
            orcRZ.put("lt",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][2]));
            orcRZ.put("ut",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][3]));
            orcRZ.put("<>",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][4]));
            orcAF90RzMap.put("Rz",orcRZ);

            Map<String,String> orcRpk=new HashMap<>();
            orcRpk.put("act",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][5]));
            orcRpk.put("lt",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][6]));
            orcRpk.put("ut",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][7]));
            orcRpk.put("<>",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][8]));
            orcAF90RzMap.put("Rpk",orcRpk);

            Map<String,String> orcRk=new HashMap<>();
            orcRk.put("act",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][9]));
            orcRk.put("lt",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][10]));
            orcRk.put("ut",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][11]));
            orcRk.put("<>",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][12]));
            orcAF90RzMap.put("Rk",orcRk);

            Map<String,String> orcRvk=new HashMap<>();
            orcRvk.put("act",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][13]));
            orcRvk.put("lt",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][14]));
            orcRvk.put("ut",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][15]));
            orcRvk.put("<>",ocrUtils.performOCRByRegion(Image,  RectAF150Constants.AF150_SURVEY_VALUE[i][16]));
            orcAF90RzMap.put("Rvk",orcRvk);
        }


        map.put(s,orcAF90RzMap);
        return map;
    }
}
